# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys



class DYSHOP:
    def __init__(self,driver):

        self.driver=driver

    def login(self):
        driver=self.driver

        # 登陆商品创建界面
        driver.get('https://fxg.jinritemai.com/index.html#/ffa/goods/create')
        # 15167681344
        time.sleep(3)
        print(driver.title)

    def create_goods(self,goods_list):
        # chrome_options = Options()
        #         # chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9999")
        #         # chrome_driver = r"C:\Users\mgxx\PycharmProjects\untitled7\autofill-core\chromedriver.exe"
        #         # driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
        driver=self.driver
        # # 登陆商品创建界面
        # driver.get('https://fxg.jinritemai.com/index.html#/ffa/goods/create')
        # time.sleep(3)
        # print(driver.title)

        # 点击服饰内衣
        time.sleep(2)
        driver.find_element_by_xpath('//div[contains(@class, "_1WP-_H3801mGbLnOedQAFD")]').click()
        # 点击女装

        driver.find_elements_by_xpath('//div[@class="t9mRgtH2nLRqIyG406qiF"]/div[@class="_1WP-_H3801mGbLnOedQAFD"]')[1].click()
        # driver.find_element_by_xpath('//*[@id="app-content"]/div/div[2]/div[2]/div/div[2]/div[2]').click()
        # 选择类目  比如 衬衫
        driver.find_element_by_xpath('//*[@id="app-content"]/div/div[2]/div[2]/div/div[3]/div[16]').click()
        # 点击下一步
        driver.find_element_by_xpath('//*[@id="app-content"]/div/div[2]/div[5]').click()

        # print('货号',goods_list[0])
        # print('品类',goods_list[1])
        # 输入商品标题

        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[2]/div[2]/div[1]//input').send_keys('{0} {1}'.format(goods_list[0],goods_list[1]))
        # # 点击上传图片
        # driver.find_element_by_xpath(
        #     '//*[@id="goodsEditScrollContainer"]/div[3]/div/div[1]/div[2]/div[1]/label/div').click()
        # time.sleep(1)
        # pyautogui.click(800, 280)
        # pyperclip.copy(r'\\172.16.151.44\案例分析共享盘\主播图片\2020.9.3-抖音小店图片\已修完（小内存/{0}'.format(goods_list[0]))
        # pyautogui.hotkey('ctrl', 'v')
        # pyautogui.press('enter')
        # pyautogui.click(1050, 500)
        # pyautogui.hotkey('ctrl', 'a')
        # pyautogui.click(1000, 740)
        # pyautogui.scroll(-1500)
        # time.sleep(5)
        # 填写售价

        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[1]/div[2]/span/span//input').send_keys('100')
        # 划线价
        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[2]/div[2]/div/span[1]/span//input').send_keys('200')

        # 商品规格 选择颜色

        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]')[0].click()
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]//input')[
            0].send_keys(Keys.DOWN)
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]//input')[
            0].send_keys(Keys.ENTER)
        # 输入规格名
        for c in range(len(goods_list[2])):
            # 添加
            time.sleep(1)
            driver.find_elements_by_xpath('//div[@class="IvBBfUnd0oniqLmaAwRq7"]')[0].click()
            driver.find_elements_by_xpath('//div[@class="GB8z7jgGKRYXCa4rpEd5l"]/div//input')[c].send_keys(goods_list[2][c])


        # 新增商品规格  尺码

        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[3]/div[2]/div[2]/button').click()
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]')[1].click()
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]//input')[
            1].send_keys(Keys.DOWN)
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]//input')[
            1].send_keys(Keys.DOWN)
        driver.find_elements_by_xpath(
            '//div[@class="_28dMRTWwkLbI1rg2qdMIXl"]/div/div/div/div[@class="ant-select-selector"]//input')[
            1].send_keys(Keys.ENTER)
        for s in range(len(goods_list[3])):
            time.sleep(0.5)
            driver.find_elements_by_xpath('//div[@class="GB8z7jgGKRYXCa4rpEd5l"]/div//input')[len(goods_list[2])+1+s].send_keys(goods_list[3][s])
            driver.find_elements_by_xpath('//div[@class="IvBBfUnd0oniqLmaAwRq7"]')[1].click()

        # 库存与价格

        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[4]/div[2]/div/div[1]/div[2]/div[3]//input').send_keys('100')

        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[4]/div[2]/div/div[1]/div[2]/div[4]//input').send_keys('0')
        # 点击设置
        driver.find_element_by_xpath(
            '//*[@id="goodsEditScrollContainer"]/div[3]/div[4]/div[2]/div/div[1]/div[2]/div[6]/button').click()
        time.sleep(5)
        driver.refresh()
        # # 保存草稿
        # driver.find_element_by_xpath('//*[@id="app-content"]/div/div[3]/div[2]').click()



if __name__=='__main__':
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9999")
    # chrome_driver = r"C:\Users\mgxx\PycharmProjects\untitled7\autofill-core\chromedriver.exe"
    chrome_driver = r"E:\chromedriver85.exe"
    driver = webdriver.Chrome(chrome_driver, options=chrome_options)
    dyshop=DYSHOP(driver)
    dyshop.login()
    goods_dict={'MA032405-2': ['MA032405-2', '套装-毛衣', ['花色'], ['S', 'M', 'L', 'XL']],
     'MQ032254': ['MQ032254', '连衣裙', ['黑色', '天蓝色'], ['XS', 'S', 'M', 'L', 'XL']],
     'MA032551-1': ['MA032551-1', '套装-衬衫', ['白色'], ['XS', 'S', 'M', 'L', 'XL']]}
    for good in goods_dict:
        dyshop.create_goods(goods_dict[good])





# # 弹窗警告
# # ant-message-custom-content ant-message-error
#
# try:
#     driver.find_element_by_xpath('//div[@class="ant-message-custom-content ant-message-error"]')
# except:
#     print('提交失败')
#     time.sleep(5)
#     driver.find_element_by_xpath('//*[@id="app-content"]/div/div[3]/div[2]').click()
#     time.sleep(1)
#     driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div/div[2]/div/div/div[2]/button').click()
# else:
#     # 我知道了
#     driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div/div[2]/div/div/div[2]/button').click()
#     driver.refresh()

